<?php
namespace Home\Controller;
use Think\Controller;
class SubmitpayController extends PublicController {
	
    public function nowpay(){
		$this->assign("k",CONTROLLER_NAME);
		$this->assign("f",ACTION_NAME);
		$this->showlist();
		$this->wxjshow();
		$this->display();
	}
	//生成订单号
	 public function buildorder(){								//把字符串分割到数组中
        $hao = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
		//array_map() 函数将用户自定义函数作用到数组中的每个值上，并返回用户自定义函数作用后的带有新值的数组。
        //ord() 函数返回字符串的首个字符的 ASCII 值。
		return $hao;
        
    }
	//把订单信息存到数据库
	public function savepro(){
		//订单号
		$cartid = $this->buildorder();
		$data['order_number'] = $cartid;
		//订单所属用户名
		$user_name =  session('name');
		$data['user'] = $user_name;
		//用户id
		$user_ids = M('user')->field("id")->where("name = '$user_name'")->find();
		$user_id = $user_ids['id'];
		$data['user_id'] = $user_id;
		$allpro = M('shoping')->where("user_id = '$user_id' and status = '1'")->select();
		foreach($allpro as $k=>$v){
			
			$allid[] = $v['p_id'];
		}
		$allids = implode(",",$allid);
		//全部商品id放进去
		$data['p_id'] = $allids;
		//生成订单时间
		$time = date("Y-m-d H:i:s");
		$data['order_time'] = $time;
		//订单状态改为已付款
		$data['status'] = 1;
		//购物车商品改为已付款
		foreach($allid as $k=>$v){
			$data1['status'] = '2';
			$proid = $v;
			var_dump($proid);
			M('shoping')->where("p_id = $proid")->save($data1);
		}
		$this->assign("cartid",$cartid);
		//存入数据库
		$info = M('order')->add($data);
	}
	//提交订单
	public function create(){
		//生成订单号
		$orderid = $this->buildorder();
		$this->savepro();
		
		$this->redirect('home/Order/myorder/?orderid='.$orderid);
		
	}
	//输出订单商品
	public function showlist(){
		$name = session('name');
		$ids = M('user')->where("name = '$name'")->find();
		$user_id = $ids['id']; 
		$allpro = M('shoping')->where("user_id = '$user_id' and status = '1'")->select();
		$this->assign("allpro",$allpro);
		
	}
	public function addToorder(){
		
		//订单包含的商品
		$name = session('name');
		//用户id
		$ids = M('user')->where("name = '$name'")->find();
		$user_id = $ids['id']; 
		$allpro = M('shoping')->where("user_id = '$user_id' and status = '1'")->select();
		foreach($allpro as $k=>$v){
			
			$allid[] = $v['p_id'];
		}
		$allid = implode(",",$allid);
		$data['p_id'] = $allid;
		
		//生成订单号
		$orderid = $this->buildorder();
		$data['order_number'] = $cartid;
		//订单所属用户名
		$data['user'] = $name;
		//订单所属用户id
		$data['user_id'] = $user_id;
		//生成订单时间
		$time = date("Y-m-d H:i:s");
		$data['order_time'] = $time;
		$data['status'] = 2;
		$this->assign("cartid",$cartid);
		//存入数据库
		$info = M('order')->add($data);
	}
}
	